<template>
  <div id="login">
    <div class="login">
      <div class="loginAvater">
        <el-icon color="#fff" style="font-size: 116rem">
          <User />
        </el-icon>
      </div>
      <div class="loginBox">
        <el-form :model="form" :rules="rules" ref="formRef">
          <el-form-item prop="username" required>
            <el-input
              class="loginIpt"
              v-model="form.username"
              placeholder="请输入已验证手机号"
              size="large"
            />
          </el-form-item>
          <el-form-item prop="password" required>
            <el-input
              class="loginIpt"
              type="password"
              v-model="form.password"
              placeholder="请输入密码"
              size="large"
            />
          </el-form-item>
        </el-form>
        <el-button
          @click="toHome(formRef)"
          class="loginBtn"
          type="primary"
          size="large"
          >登 录</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup>
import { User } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import loginHttp from "@/https/login.js";
import Storage from "@/utils/storage.js"; // storage工具类，简单的封装
import { ElMessage } from "element-plus";
const formRef = ref();
const router = useRouter();
const route = useRoute();
const rules = reactive({
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});
const form = reactive({
  username: "",
  password: "",
});
const toHome = async (formRef) => {
  if (!formRef) return;
  await formRef.validate((valid, fields) => {
    if (valid) {
      loginHttp.login(form).then((res) => {
        if (res.data.code == 200) {
          Storage.localSet("tokenValue", res.data.data.tokenInfo.tokenValue);
          Storage.localSet("tokenName", res.data.data.tokenInfo.tokenName);
          Storage.localSet("loginId", res.data.data.tokenInfo.loginId);
          Storage.localSet("depId", res.data.data.depId);
          Storage.localSet("username", res.data.data.username);
          ElMessage({
            message: "登录成功",
            type: "success",
          });
          router.push("/main");
        }
      });
    } else {
      // console.log('error')
    }
  });
};
</script>

<style scoped lang="less">
#login {
  height: 100%;
  width: 100%;
  background: #fafafa;
}

.login {
  width: 100%;
  height: 50%;
  background: #91caff;
  position: relative;

  .loginAvater {
    position: absolute;
    left: 50%;
    top: 20%;
    transform: translateX(-50%);
    width: 140rem;
    height: 140rem;
    border: 6rem solid #fff;
    border-radius: 50%;
    overflow: hidden;
    box-sizing: border-box;
    padding-top: 16rem;
    text-align: center;
  }

  .loginBox {
    position: absolute;
    width: 400rem;
    bottom: -185rem;
    padding: 60rem;
    left: 50%;
    transform: translateX(-50%);
    background: #fff;

    .loginIpt {
      margin-top: 20rem;
      margin-bottom: 5rem;
      height: 48rem;
    }

    .loginBtn {
      width: 400rem;
      height: 48rem;
      background: #91caff;
      border: none;
      margin-top: 20rem;
    }
  }
}
</style>
